package com.nd.teamfile.fileexplorer.sdk.type;

import java.io.File;
import java.util.HashMap;

import android.content.Intent;
import android.net.Uri;

import com.common.android.utils.parser.BaseType;
import com.nd.teamfile.fileexplorer.R;

/**
 * 文件类型，通过这个类获其后缀名对应的文件图标和打开它需要的Intent
 * 
 * @author mk
 * 
 */
public class FileType implements BaseType {

	private final HashMap<String, MIME_SOURCE> MIME_MapTable = new HashMap<String, MIME_SOURCE>() {

		/**
		 * 
		 */
		private static final long serialVersionUID = 6509951825930096927L;

		{
			put(".3gp", MIME_SOURCE._3GP);
			put(".asf", MIME_SOURCE.ASF);
			put(".avi", MIME_SOURCE.AVI);
			put(".bin", MIME_SOURCE.BIN);
			put(".bmp", MIME_SOURCE.BMP);
			put(".c", MIME_SOURCE.C);
			put(".class", MIME_SOURCE.CLASS);
			put(".conf", MIME_SOURCE.CONF);
			put(".cpp", MIME_SOURCE.CPP);
			put(".doc", MIME_SOURCE.DOC);
			put(".exe", MIME_SOURCE.EXE);
			put(".gif", MIME_SOURCE.GIF);
			put(".gtar", MIME_SOURCE.GTAR);
			put(".gz", MIME_SOURCE.GZ);
			put(".h", MIME_SOURCE.H);
			put(".htm", MIME_SOURCE.HTM);
			put(".html", MIME_SOURCE.HTML);
			put(".jar", MIME_SOURCE.JAR);
			put(".java", MIME_SOURCE.JAVA);
			put(".jpeg", MIME_SOURCE.JPEG);
			put(".jpg", MIME_SOURCE.JPG);
			put(".js", MIME_SOURCE.JS);
			put(".log", MIME_SOURCE.LOG);
			put(".m3u", MIME_SOURCE.M3U);
			put(".m4a", MIME_SOURCE.M4A);
			put(".m4b", MIME_SOURCE.M4B);
			put(".m4p", MIME_SOURCE.M4P);
			put(".m4u", MIME_SOURCE.M4U);
			put(".m4v", MIME_SOURCE.M4V);
			put(".mov", MIME_SOURCE.MOV);
			put(".mp2", MIME_SOURCE.MP2);
			put(".mp3", MIME_SOURCE.MP3);
			put(".mp4", MIME_SOURCE.MP4);
			put(".mpc", MIME_SOURCE.MPC);
			put(".mpe", MIME_SOURCE.MPE);
			put(".mpeg", MIME_SOURCE.MPEG);
			put(".mpg", MIME_SOURCE.MPG);
			put(".mpg4", MIME_SOURCE.MPG4);
			put(".mpga", MIME_SOURCE.MPGA);
			put(".msg", MIME_SOURCE.MSG);
			put(".ogg", MIME_SOURCE.OGG);
			put(".pdf", MIME_SOURCE.PDF);
			put(".png", MIME_SOURCE.PNG);
			put(".pps", MIME_SOURCE.PPS);
			put(".ppt", MIME_SOURCE.PPT);
			put(".prop", MIME_SOURCE.PROP);
			put(".rar", MIME_SOURCE.RAR);
			put(".rc", MIME_SOURCE.RC);
			put(".rmvb", MIME_SOURCE.RMVB);
			put(".rtf", MIME_SOURCE.RTF);
			put(".sh", MIME_SOURCE.SH);
			put(".tar", MIME_SOURCE.TAR);
			put(".tgz", MIME_SOURCE.TGZ);
			put(".txt", MIME_SOURCE.TXT);
			put(".wav", MIME_SOURCE.WAV);
			put(".wma", MIME_SOURCE.WMA);
			put(".wmv", MIME_SOURCE.WMV);
			put(".wps", MIME_SOURCE.WPS);
			put(".xml", MIME_SOURCE.XML);
			put(".z", MIME_SOURCE.Z);
			put(".zip", MIME_SOURCE.ZIP);
			put(".apk",MIME_SOURCE.APK);
			put(".rss", MIME_SOURCE.RSS);
			put(".docx",MIME_SOURCE.DOCX);
			put(".xls",MIME_SOURCE.XLS);
			put(".xlsx",MIME_SOURCE.XLS);
			put(".xlsm",MIME_SOURCE.XLS);
			put(".xlt",MIME_SOURCE.XLT);
			put(".vsd",MIME_SOURCE.VSD);
			put(".torrent",MIME_SOURCE.TORRENT);
			put(".taz",MIME_SOURCE.TAZ);
			put(".xhtm",MIME_SOURCE.XHTML);
			put(".3gpp",MIME_SOURCE._3GPP);
			put(".amr",MIME_SOURCE.AMR);
			put(".mpega",MIME_SOURCE.MPEGA);
			put(".rm",MIME_SOURCE.RM);
			put(".jpe",MIME_SOURCE.JPE);
			put(".wbmp",MIME_SOURCE.WBMP);
			put(".psd",MIME_SOURCE.PSD);
			put(".text",MIME_SOURCE.TEXT);
			put(".3g2",MIME_SOURCE._3G2);
			put(".vob",MIME_SOURCE.VOB);
		}
	};

	public static enum MIME_SOURCE {
		
		TAZ("application/x-gtar",R.drawable.zip,R.drawable.large_zip_type_icon),
		XHTML("application/xhtml+xml",R.drawable.file,R.drawable.large_file_type_icon),
		_3GPP("audio/3gpp",R.drawable.music,R.drawable.large_music_type_icon),
		AMR("audio/amr",R.drawable.music,R.drawable.large_music_type_icon),
		MPEGA("audio/mpeg",R.drawable.photo,R.drawable.large_photo_type_icon),
		RM("audio/x-pn-realaudio",R.drawable.unknow,R.drawable.large_unknown_type_icon),
		JPE("image/jpeg",R.drawable.photo,R.drawable.large_photo_type_icon),
		WBMP("image/vnd.wap.wbmp",R.drawable.photo,R.drawable.large_photo_type_icon),
		PSD("image/x-photoshop",R.drawable.photo,R.drawable.large_photo_type_icon),
		TEXT("text/plain",R.drawable.file,R.drawable.large_file_type_icon),
		_3G2("video/3gpp",R.drawable.unknow,R.drawable.large_unknown_type_icon),
		VOB("video/mpeg",R.drawable.music,R.drawable.large_music_type_icon),
		RSS("application/rss+xml",R.drawable.file,R.drawable.large_file_type_icon),
		DOCX("application/vnd.openxmlformats-officedocument.wordprocessingml.document",R.drawable.word,R.drawable.large_word_type_icon),
		XLS("application/vnd.ms-excel",R.drawable.excel,R.drawable.large_excel_type_icon),
		XLT("application/vnd.ms-excel",R.drawable.excel,R.drawable.large_excel_type_icon),
		VSD("application/vnd.visio",R.drawable.unknow,R.drawable.large_unknown_type_icon),
		TORRENT("application/x-bittorrent",R.drawable.file,R.drawable.large_file_type_icon),
		
		_3GP("video/3gpp", R.drawable.music, R.drawable.large_music_type_icon), APK(
				"application/vnd.android.package-archive", R.drawable.app,
				R.drawable.large_app_type_icon), ASF("video/x-ms-asf",
				R.drawable.music, R.drawable.large_music_type_icon), AVI(
				"video/x-msvideo", R.drawable.music,
				R.drawable.large_music_type_icon), BIN(
				"application/octet-stream", R.drawable.file,
				R.drawable.large_file_type_icon), BMP("image/bmp",
				R.drawable.photo, R.drawable.large_photo_type_icon), C(
				"text/plain", R.drawable.txt, R.drawable.large_txt_type_icon), CLASS(
				"application/octet-stream", R.drawable.file,
				R.drawable.large_file_type_icon), CONF("text/plain",
				R.drawable.txt, R.drawable.large_txt_type_icon), CPP(
				"text/plain", R.drawable.txt, R.drawable.large_txt_type_icon), DOC(
				"application/msword", R.drawable.word,
				R.drawable.large_word_type_icon), EXE(
				"application/octet-stream", R.drawable.app,
				R.drawable.large_app_type_icon), GIF("image/gif",
				R.drawable.photo, R.drawable.large_photo_type_icon), GTAR(
				"application/x-gtar", R.drawable.zip,
				R.drawable.large_zip_type_icon), GZ("application/x-gzip",
				R.drawable.zip, R.drawable.large_zip_type_icon), H(
				"text/plain", R.drawable.txt, R.drawable.large_txt_type_icon), HTM(
				"text/html", R.drawable.txt, R.drawable.large_txt_type_icon), HTML(
				"text/html", R.drawable.txt, R.drawable.large_txt_type_icon), JAR(
				"application/java-archive", R.drawable.zip,
				R.drawable.large_zip_type_icon), JAVA("text/plain",
				R.drawable.txt, R.drawable.large_txt_type_icon), JPEG(
				"image/jpeg", R.drawable.photo,
				R.drawable.large_photo_type_icon), JPG("image/jpeg",
				R.drawable.photo, R.drawable.large_photo_type_icon), JS(
				"application/x-javascript", R.drawable.file,
				R.drawable.large_file_type_icon), LOG("text/plain",
				R.drawable.txt, R.drawable.large_txt_type_icon), M3U(
				"audio/x-mpegurl", R.drawable.music,
				R.drawable.large_music_type_icon), M4A("audio/mp4a-latm",
				R.drawable.music, R.drawable.large_music_type_icon), M4B(
				"audio/mp4a-latm", R.drawable.music,
				R.drawable.large_music_type_icon), M4P("audio/mp4a-latm",
				R.drawable.music, R.drawable.large_music_type_icon), M4U(
				"video/vnd.mpegurl", R.drawable.music,
				R.drawable.large_music_type_icon), M4V("video/x-m4v",
				R.drawable.music, R.drawable.large_music_type_icon), MOV(
				"video/quicktime", R.drawable.music,
				R.drawable.large_music_type_icon), MP2("audio/x-mpeg",
				R.drawable.music, R.drawable.large_music_type_icon), MP3(
				"audio/x-mpeg", R.drawable.music,
				R.drawable.large_music_type_icon), MP4("video/mp4",
				R.drawable.music, R.drawable.large_music_type_icon), MPC(
				"application/vnd.mpohun.certificate", R.drawable.file,
				R.drawable.large_file_type_icon), MPE("video/mpeg",
				R.drawable.music, R.drawable.large_music_type_icon), MPEG(
				"video/mpeg", R.drawable.music,
				R.drawable.large_music_type_icon), MPG("video/mpeg",
				R.drawable.music, R.drawable.large_music_type_icon), MPG4(
				"video/mp4", R.drawable.music, R.drawable.large_music_type_icon), MPGA(
				"audio/mpeg", R.drawable.music,
				R.drawable.large_music_type_icon), MSG(
				"application/vnd.ms-outlook", R.drawable.file,
				R.drawable.large_file_type_icon), OGG("audio/ogg",
				R.drawable.music, R.drawable.large_music_type_icon), PDF(
				"application/pdf", R.drawable.pdf,
				R.drawable.large_pdf_type_icon), PNG("image/png",
				R.drawable.photo, R.drawable.large_photo_type_icon), PPS(
				"application/vnd.ms-powerpoint", R.drawable.file,
				R.drawable.large_file_type_icon), PPT(
				"application/vnd.ms-powerpoint", R.drawable.ppt,
				R.drawable.large_ppt_type_icon), PROP("text/plain",
				R.drawable.txt, R.drawable.large_txt_type_icon), RAR(
				"application/x-rar-compressed", R.drawable.file,
				R.drawable.large_file_type_icon), RC("text/plain",
				R.drawable.txt, R.drawable.large_txt_type_icon), RMVB(
				"audio/x-pn-realaudio", R.drawable.music,
				R.drawable.large_music_type_icon), RTF("application/rtf",
				R.drawable.file, R.drawable.large_file_type_icon), SH(
				"text/plain", R.drawable.txt, R.drawable.large_txt_type_icon), TAR(
				"application/x-tar", R.drawable.file,
				R.drawable.large_file_type_icon), TGZ(
				"application/x-compressed", R.drawable.zip,
				R.drawable.large_zip_type_icon), TXT("text/plain",
				R.drawable.txt, R.drawable.large_txt_type_icon), WAV(
				"audio/x-wav", R.drawable.music,
				R.drawable.large_music_type_icon), WMA("audio/x-ms-wma",
				R.drawable.music, R.drawable.large_music_type_icon), WMV(
				"audio/x-ms-wmv", R.drawable.music,
				R.drawable.large_music_type_icon), WPS(
				"application/vnd.ms-works", R.drawable.file,
				R.drawable.large_file_type_icon), XML("text/plain",
				R.drawable.txt, R.drawable.large_txt_type_icon), Z(
				"application/x-compress", R.drawable.zip,
				R.drawable.large_zip_type_icon), ZIP("application/zip",
				R.drawable.zip, R.drawable.large_zip_type_icon);

		String mimeType;
		int sourceId;
		int largeSourceId;

		MIME_SOURCE(String mimeType, int sourceId, int largeSourceId) {
			this.mimeType = mimeType;
			this.sourceId = sourceId;
			this.largeSourceId = largeSourceId;
		}
	}

	private File file;
	private String name;
	private MIME_SOURCE mimeSource = null;

	public FileType(File file) {
		this.file = file;
		initMIME();
	}

	public FileType(String name) {
		this.name = name;
		initMIME();
	}

	public Intent getIntent() {
		if (mimeSource == null || file == null) {
			return null;
		}
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// 设置intent的Action属性
		intent.setAction(Intent.ACTION_VIEW);
		// 获取文件file的MIME类型
		// 设置intent的data和Type属性。
		intent.setDataAndType(/* uri */Uri.fromFile(file), mimeSource.mimeType);
		return intent;
	}

	public int getSourceId() {
		return mimeSource == null ? R.drawable.file : mimeSource.sourceId;
	}

	public int getLargeSourceId() {
		return mimeSource == null ? R.drawable.large_unknown_type_icon
				: mimeSource.largeSourceId;
	}

	/**
	 * 根据文件后缀名获得对应的MIME类型。
	 * 
	 * @param file
	 */
	private void initMIME() {
		String pathOrName;
		if (file == null) {
			pathOrName = name;
		} else {
			pathOrName = file.getName();
		}
		// 获取后缀名前的分隔符"."在path中的位置。
		int dotIndex = pathOrName.lastIndexOf(".");
		if (dotIndex < 0) {
			return;
		}
		/* 获取文件的后缀名 */
		String end = pathOrName.substring(dotIndex, pathOrName.length())
				.toLowerCase();
		if (end.equals("")) {
			return;
		}
		// 在MIME和文件类型的匹配表中找到对应的MIME类型。
		mimeSource = MIME_MapTable.get(end);
	}

	public String getMIMIE() {
		String pathOrName;
		if (file == null) {
			pathOrName = name;
		} else {
			pathOrName = file.getName();
		}
		// 获取后缀名前的分隔符"."在path中的位置。
		int dotIndex = pathOrName.lastIndexOf(".");
		if (dotIndex < 0) {
			return "";
		}
		/* 获取文件的后缀名 */
		String end = pathOrName.substring(dotIndex, pathOrName.length())
				.toLowerCase();
		if (end.equals("")) {
			return "";
		}
		// 在MIME和文件类型的匹配表中找到对应的MIME类型。
		MIME_SOURCE mimeSource = MIME_MapTable.get(end);
		return mimeSource.mimeType;
	}

}
